package com.chong.activiti.controller;

import com.chong.activiti.dto.DeclareDto;
import com.chong.activiti.model.Activiti;
import com.chong.activiti.model.ActivitiTask;
import com.chong.activiti.model.ResponseMsg;
import com.chong.activiti.service.ActivitiService;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * @author chong
 * @create 2020/6/4
 * Desc: 流程接口
 */
@Api(description = "流程接口")
@Slf4j
@RestController
public class TaskController {
    @Autowired
    private ActivitiService activitiService;

    /**
     * 启动发起流程
     * @param dto
     * @return
     */
    @ApiOperation(value = "启动发起流程")
    @RequestMapping(value = "/startFlow", method = RequestMethod.POST)
    public ResponseMsg multi(@RequestBody DeclareDto dto) {
        ResponseMsg msg = new ResponseMsg();
        msg.setSuccess(true);
        try {
            ProcessInstance pi = activitiService.getTask(dto.getActiviti().getAuditor(),dto.getTempName(),dto.getMap());
            Map<String,Object> map = Maps.newHashMap();
            map.put("id",pi.getId());
            map.put("name",pi.getName());
            msg.setData(map);
        } catch (Exception e) {
            msg.setSuccess(false);
            log.error(e.getMessage());
        }
        return msg;
    }
    /**
     * 查询当前人需要审批的任务
     * @param userId
     * @return
     */
    @RequestMapping(value = "/myActiviti", method = RequestMethod.POST)
    public ResponseMsg myActiviti(@RequestBody String userId) {
        ResponseMsg msg = new ResponseMsg();
        msg.setSuccess(true);
        try {
            List<Activiti> list = activitiService.myActiviti(userId);
            msg.setData(list);
        } catch (Exception e) {
            msg.setSuccess(false);
            log.error(e.getMessage());
        }
        return msg;
    }

    /**
     * 查询待审批的任务
     * @param userId
     * @return
     */
    @ApiOperation(value = "查询待审批的任务")
    @RequestMapping(value = "/myApproval", method = RequestMethod.POST)
    public ResponseMsg myApproval(@RequestBody String userId) {
        ResponseMsg msg = new ResponseMsg();
        msg.setSuccess(true);
        try {
            List<ActivitiTask> list = activitiService.myApproval(userId);
            msg.setData(list);
        } catch (Exception e) {
            msg.setSuccess(false);
            log.error(e.getMessage());
        }
        return msg;
    }

    /**
     * 审核处理任务
     * @param activitiTask
     * @return
     */
    @ApiOperation(value = "审核处理任务")
    @RequestMapping(value = "/passApproval", method = RequestMethod.POST)
    public Object passApproval(@RequestBody ActivitiTask activitiTask) {
        ResponseMsg msg = new ResponseMsg();
        msg.setSuccess(true);
        try {
            activitiService.passApproval(activitiTask.getActiviti().getAuditor(), activitiTask);
        } catch (Exception e) {
            log.error(e.getMessage());
            msg.setSuccess(false);
        }
        return msg;
    }

    /**
     * 查询用户发起的流程
     * @param userId
     * @return
     */
    @ApiOperation(value = "我发起的流程")
    @PostMapping("myActivitiRecord")
    public ResponseMsg myActivitiRecord(@RequestBody String userId) {
        ResponseMsg msg = new ResponseMsg();
        msg.setSuccess(true);
        try {
            List<Activiti> list = activitiService.myActivitiRecord(userId);
            msg.setData(list);
        } catch (Exception e) {
            log.error(e.getMessage());
            msg.setSuccess(false);
        }
        return msg;
    }

    /**
     * 我的审核记录
     * @param userName
     * @return
     */
    @ApiOperation(value = "我的审核记录")
    @RequestMapping(value = "/myApprovalRecord", method = RequestMethod.POST)
    public ResponseMsg myApprovalRecord(@RequestBody String userName) {
        ResponseMsg msg = new ResponseMsg();
        msg.setSuccess(true);
        try {
            List<Activiti> list = activitiService.myApprovalRecord(userName);
            msg.setData(list);
        } catch (Exception e) {
            msg.setSuccess(false);
            log.error(e.getMessage());
        }
        return msg;
    }

    /**
     * 生成流程图片
     * @param processDefinitionId
     * @return
     */
    @ApiOperation(value = "查看流程图")
    @RequestMapping(value = "/tracePhoto", method = RequestMethod.POST)
    public ResponseMsg tracePhoto(@RequestBody String processDefinitionId) {
        ResponseMsg msg = new ResponseMsg();
        msg.setSuccess(true);
        try {
            activitiService.tracePhoto(processDefinitionId, new FileOutputStream("e:/a.png"));
        } catch (FileNotFoundException e) {
            msg.setSuccess(false);
            log.error(e.getMessage());
        }
        return msg;
    }
}
